package com.xbongbong.paas.script.aspect;

import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.sys.enums.JobStatusEnum;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Component
@Aspect
public class AutoJobAspect {
    @Resource
    private PaasRedisHelper paasRedisHelper;

    private static String filePrefix = "autoJob";

    @Pointcut("@annotation(com.xbongbong.paas.annotation.AutoJob)")
    public void pointCut(){};

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String url = request.getRequestURI();
        paasRedisHelper.setValue(filePrefix,url, JobStatusEnum.RUNNING.getCode(), TimeConstant.SECONDS_TWO_HOURS);
        try {
            Object returnObj = joinPoint.proceed();
            paasRedisHelper.setValue(filePrefix,url, JobStatusEnum.FINISH.getCode(), TimeConstant.SECONDS_TWO_HOURS);
            return returnObj;
        } catch (Throwable throwable) {
            throw throwable;
        }
    }
}
